<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UsertypeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/StaisCom.php");
require_once(dirname(__FILE__) . "/../dbcom/WorkerCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");
require_once(dirname(__FILE__) . "/../dbcom/C_serviceCom.php");

$log = new Logger("./../worderapi.log");
$common = new Common();
$ordercom = new OrderCom();
$reservetimecom = new ReservetimeCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();
$servicecom = new ServiceCom();
$staiscom = new StaisCom();
$workercom = new WorkerCom();
$appointcom = new AppointCom();
$serviceinfocom = new ServiceinfoCom();
$cservicecom = new CservicetCom();

if (!isset($_REQUEST['cont']) || empty($_REQUEST['cont'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot request cont"));
    exit();
}

switch ($_REQUEST['cont']) {
    case 1://预约列表
        if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot get userid"));
            exit();
        }
        $workerid = $_REQUEST['userid'];
        $worker = $workercom->getUserById($workerid);
        if (!$worker) {
            echo json_encode(array("code" => 400, "msg" => "cannot get workerinfo"));
            exit();
        }
        $appointlist = $appointcom->getAppointByCid($worker['community_id']);
        if (!$appointlist) {
            echo json_encode(array("code" => 400, "msg" => "cannot get appointlist"));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        foreach ($appointlist as $k => $v) {
            //获取最新的预约时间
            $now = strtotime(date("Y-m-d", time()));
            if ($v['time'] < $now) {
                continue;
            }
            $aa = $reservetimecom->getRtimeById($v['reservetime_id']);
            $time = date("m-d", $v['time']);
            $result["data"][$k]["reservetime"] = $time . " " . $aa['info'];
            $result["data"][$k]['id'] = $v['id'];
            $result["data"][$k]['address'] = $v['address'];
            $result["data"][$k]['status'] = $v['status'];
            $result["data"][$k]['floor'] = $v['floor'];
        }
        echo json_encode($result);
        break;
    case 2://订单操作确认
        if (!isset($_REQUEST['appointid']) || empty($_REQUEST['appointid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot request orderid"));
            exit();
        }
        $appointid = $_REQUEST['appointid'];
        $appoint = $appointcom->getAppointById($appointid);
        if (!$appoint) {
            echo json_encode(array("code" => 400, "msg" => "get appoint failed"));
            exit();
        }
        $res = $appointcom->changeStatus($appointid, 1);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "change appoint status failed"));
        }
        break;
    case 3://订单操作取消
        if (!isset($_REQUEST['appointid']) || empty($_REQUEST['appointid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot request appointid"));
            exit();
        }
        $appointid = $_REQUEST['appointid'];
        $appoint = $appointcom->getAppointById($appointid);
        if (!$appoint) {
            echo json_encode(array("code" => 400, "msg" => "get appoint failed"));
            exit();
        }
        $a = $cservicecom->getCservice($appoint['customer_id'], $appoint['service_id']);
        $number = $a['number']++;
        $b = $cservicecom->changeNumber($appoint['customer_id'], $appoint['service_id'], $number);
        if(!$b){
            echo json_encode(array("code" => 400, "msg" => "change number failed"));
            exit();
        }
        $res = $appointcom->changeStatus($appointid, 2);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "change appoint status failed"));
        }
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined failed"));
        break;
}

exit();
